var express = require("express");
var router = express.Router();
const http = require("http");
const { UserModel, PermissionsModel } = require("../model/user");
const { orderModel } = require("../model/order");
const { BrandModel } = require("../model/model");
const { Server } = require("socket.io");
const app = express();
const server = http.createServer(app);
const io = new Server(server, {
  cors: {
    origin: "http://localhost:5173",
    methods: ["GET", "POST"],
  },
  // 设置心跳
  pingInterval: 60000,
  pingTimeout: 30000,
});
io.on("connection", (socket) => {
  console.log("socket连接成功");
  socket.on("changeData", async (data) => {
    const BrandData = await BrandModel.find().skip(0).limit(10);
    socket.emit("getData", JSON.stringify(BrandData));
  });
  socket.on("heartbeat", () => {
    console.log("心跳");
  });

  socket.on("disconnect", () => {
    console.log("用户断开连接");
  });
});
io.listen(4000);

router.get("/getUser", async (req, res) => {
  const { pageNum, pageSize, username, phone, email } = req.query;
  const search = [];
  if (username) {
    search.push({ username: new RegExp(username, "i") });
  }
  if (phone) {
    search.push({ phone: new RegExp(phone, "i") });
  }
  if (email) {
    search.push({ email: new RegExp(email, "i") });
  }
  const skip = (pageNum - 1) * pageSize;
  const length = (await UserModel.find({})).length;
  let data = [];
  if (search.length > 0) {
    data = await UserModel.find({ $and: search })
      .skip(skip)
      .limit(pageSize)
      .populate("permissionsId");
  } else {
    data = await UserModel.find({})
      .skip(skip)
      .limit(pageSize)
      .populate("permissionsId");
  }
  const PermissionsData = await PermissionsModel.find();
  res.send({
    code: 200,
    data: {
      userData: data,
      PermissionsData: PermissionsData,
      length,
    },
    msg: "获取成功",
  });
});

router.delete("/delUser", async (req, res) => {
  const { id } = req.query;
  await UserModel.deleteOne({ _id: id });
  console.log(id);
  res.send({
    code: 200,
    msg: "删除成功",
  });
});

router.put("/updatePermissions", async (req, res) => {
  const { id, data } = req.body;
  await UserModel.findByIdAndUpdate(id, { permissionsId: data });
  res.send({
    code: "200",
    msg: "修改成功",
  });
});

router.put("/updateStatus", async (req, res) => {
  const { id, status } = req.body;
  await UserModel.findByIdAndUpdate(id, { status: status });
  res.send({
    code: "200",
    msg: "修改启用状态成功",
  });
});

router.get("/getRole", async (req, res) => {
  const PermissionsData = await PermissionsModel.find();
  res.send({
    code: 200,
    data: PermissionsData,
    msg: "获取成功",
  });
});

router.post("/updateRole", async (req, res) => {
  const { id, MenuList } = req.body;
  await PermissionsModel.findByIdAndUpdate(id, { MenuList: MenuList });
  res.send({
    code: 200,
    msg: "修改权限成功",
  });
});

router.post("/addUser", async (req, res) => {
  const data = req.body;
  data.addTime = new Date();
  data.lastLogin = new Date();
  await UserModel.create(data);
  res.send({
    code: 200,
    msg: "添加成功！",
  });
});

router.get("/getOrders", async (req, res) => {
  const data = await orderModel.find();
  res.send({
    code: 200,
    msg: "获取成功！",
    data,
  });
});
module.exports = router;
